package org.prs.web.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.prs.model.Child;
import org.prs.model.Guardian;
import org.prs.model.Medical;
import org.prs.persistence.ChildDatabasePersitenceManager;
import org.prs.persistence.GuardianDatabasePersistence;
import org.prs.persistence.MedicalDatabasePersistence;

public class ChildUpdateServlet extends HttpServlet {

    private final String CALL_UPDATE_GUARDIAN = "updateguardian";
    private final String CALL_ADD_GUARDIAN = "addguardian";
    private final String CALL_ADD_MEDICAL_DATA = "addmedicaldata";
    private ChildDatabasePersitenceManager persitChild = new ChildDatabasePersitenceManager();
    private GuardianDatabasePersistence persitGuardian = new GuardianDatabasePersistence();
    private Guardian guardian = new Guardian();
    private Child child = new Child();
    MedicalDatabasePersistence persistMedical=new MedicalDatabasePersistence();

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        String call = request.getPathInfo().substring(1);
        PrintWriter out = response.getWriter();

        try {
            JSONObject result = new JSONObject();
            if (CALL_UPDATE_GUARDIAN.equals(call)) {
                guardian = new Guardian();
                guardian = persitGuardian.getGuardianById(Integer.parseInt(request.getParameter("guardianid")));
                guardian.setFirstName(request.getParameter("firstname"));
                guardian.setLastName(request.getParameter("lastname"));
                guardian.setMiddleName(request.getParameter("middlename"));
                boolean updateGuardian = persitGuardian.updateGuardian(guardian);
                if (updateGuardian) {
                    result = new JSONObject();
                    JSONObject name = new JSONObject();

                    JSONArray names = new JSONArray();
                    name.put("firstname", guardian.getFirstName());
                    name.put("lastname", guardian.getLastName());
                    name.put("middlename", guardian.getMiddleName());
                    names.add(name);
                    result.put("names", names);
                }
            }
            if (CALL_ADD_GUARDIAN.equals(call)) {
                guardian = new Guardian();
                guardian.setChildId(Integer.parseInt(request.getParameter("childid")));
                guardian.setFirstName(request.getParameter("firstname"));
                guardian.setLastName(request.getParameter("lastname"));
                guardian.setMiddleName(request.getParameter("middlename"));
                guardian.setPhoneNumber(request.getParameter("phonenumber"));
                guardian.setRelationshipTypeId(Integer.parseInt(request.getParameter("relationship")));
                boolean insertGuardian = persitGuardian.insertGuardian(guardian);
                if (insertGuardian) {
                    result = new JSONObject();
                    JSONObject name = new JSONObject();

                    JSONArray names = new JSONArray();
                    name.put("firstname", guardian.getFirstName());
                    name.put("lastname", guardian.getLastName());
                    name.put("middlename", guardian.getMiddleName());
                    name.put("phonenumber", guardian.getPhoneNumber());
                    name.put("relationship", guardian.getRelationshipTypeId());
                    name.put("guardianid", persitGuardian.getChildGuardians(guardian.getChildId()).size());
                    names.add(name);
                    result.put("names", names);
                }
            }
            if (CALL_ADD_MEDICAL_DATA.equals(call)) {
                Medical medical=new Medical();
                medical.setAilment(request.getParameter("ailment"));
                medical.setDoctorComment(request.getParameter("doctorcomment"));
                medical.setHospitalVisited(request.getParameter("hospitalvisited"));
                medical.setCost(Double.parseDouble(request.getParameter("cost")));
                medical.setChildId(Integer.parseInt(request.getParameter("childid")));
                boolean da=persistMedical.insertHealthAssessment(medical);
            }
            if (result != null) {
                response.setHeader("cache-control", "no-cache");
                response.setCharacterEncoding("utf-8");
                response.setContentType("application/json");
                result.write(out);
                out.close();
            }
        } finally {
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
